package xyz.sicau.blog.controller;

import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.sicau.blog.bean.User;
import xyz.sicau.blog.dao.UserMapper;
import xyz.sicau.blog.service.UserService;
import xyz.sicau.blog.utils.JwtUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@Api(tags = "用户登录和获取信息接口")
@RequestMapping("/user")
public class UserController {
    @Resource
    UserService userService;
    @Resource
    UserMapper userMapper;

    /**
     * 查询用户信息===================================================
     */
    @RequestMapping("/user_by_token")
    public User userByToken(String usertoken) {
        int userId = JwtUtils.getUserIdByToken(usertoken);
        return userMapper.getUserById(userId);
    }

    @RequestMapping("/user_by_name")
    public User userByName(String username) {
        return userMapper.getUserByName(username);
    }

    @RequestMapping("/user_by_id")
    public User userByUserid(int userid) {
        return userMapper.getUserById(userid);
    }

    /**
     * 获取用户设置=======================================================
     */
    // 用户设置
    @RequestMapping("/getSetting")
    String getSetting(String usertoken) {
        int userId = JwtUtils.getUserIdByToken(usertoken);
        System.out.println("/user/getSetting: userId " + JwtUtils.getUserIdByToken(usertoken));
        return userMapper.getUserSetting(userId);
    }

    /**
     * 登录注册接口========================================================
     */
    @RequestMapping("/login")
    public String login(String username, String password) {
        System.out.println("===login请求运行==============================");
        if (userMapper.countUserByNameAndPswd(username, password) == 1) {
            int userId = userMapper.getUserIdByName(username);
            return JwtUtils.getToken(userId, userMapper.getUserNameById(userId));
        } else {
            return "0";
        }
    }

    @RequestMapping("/regist")
    public int regist(String username, String password,
                      String clientcheckcode, HttpServletRequest request) {
        System.out.println("===regist请求运行==============================");
        String serverCheckcode = (String) request.getServletContext().getAttribute("random");

        if (clientcheckcode.equalsIgnoreCase(serverCheckcode)) {//比较验证码，不区分大小写
            if (userMapper.getUsersByName(username) != 0) {
                System.out.println("用户名已存在");
                return 0;
            }
            // 注册用户
            return userService.registUser(username, password);
        } else {
            System.out.println("验证码验证失败！");
            return 0;
        }
    }

    // QQ登录后注册接口，直接返回token
    @RequestMapping("/regist_qq")
    public String registQ(String username, String password) {
        if (userMapper.getUsersByName(username) != 0) {
            System.out.println("用户名已存在");
            return "0";
        }
        // 注册用户，直接返回token
        if (userService.registUser(username, password) > 0) {
            return JwtUtils.getToken(userMapper.getUserIdByName(username), username);
        } else {
            return "0";
        }

    }

    // 留给 ajax 的验证码预查询
//    @RequestMapping("/checkcode")
//    public int checkcode(String clientcheckcode, HttpServletRequest request) {
//        String serverCheckcode = (String) request.getServletContext().getAttribute("random");
//        return clientcheckcode.equalsIgnoreCase(serverCheckcode)?1:0;//比较验证码，不区分大小写
//    }

    /**
     * QQ登录接口
     * 查询openid的用户，如果有直接返回token，如果没有返回0
     *
     * @param openid 回传的QQ openid
     */
    @RequestMapping("/getUsersCountByOpenidQ")
    public String getUserCountByOpenidQ(String openid) {
        System.out.println(openid);
        User user = userMapper.getUserByOpenidQ(openid);
        System.out.println(user);
        if (user == null) return "0";
        return JwtUtils.getToken(user.getUserId(), user.getUserName());
    }

    // 绑定openid
    @RequestMapping("/bind_openid")
    public int bindOenpid(String openid, String usertoken) {
        System.out.println(openid + " " + usertoken);
        int userId = JwtUtils.getUserIdByToken(usertoken);
        return userMapper.bindOpenidById(userId, openid);
    }


    // 通过用户名，查询有多少个同名用户，注册时要用
    @RequestMapping("/getUsersCountByUserName")
    public int getUsersCountByUserName(String username) {
        return userMapper.getUsersByName(username);
    }


}
